package com.yanqu.road.dao.impl.activity.yqhotel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.yqhotel.data.YqHotelEventData;
import com.yanqu.road.entity.activity.yqhotel.data.YqHotelUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class YqHotelUserDataDaoImpl extends TempDataObjectDao<YqHotelUserData> {
    public static String getYqHotelUserDataSql = "select * from `t_u_yq_hotel_user_data` where `activityId`=? and `userId`=?";
    public YqHotelUserData getYqHotelUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getYqHotelUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateYqHotelUserDataSql = "update `t_u_yq_hotel_user_data` set integral=?,studentCount=?,buildingLevelMap=?,buildingLevelUpTimeMap=?,skillLevelMap=?,studentMap=?,cacheDisappearMap=?,solderData=?,solderTitleCountMap=?,dieCountMap=?,lastEventTime=?,eventMap=?,produceTime=?,updateTime=?,bossId=?,bossKillTimes=?,bossBlood=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(YqHotelUserData yqHotelUserData) {
        yqHotelUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, yqHotelUserData.getIntegral()));
        para.put(i++, new DbParameter(Types.INTEGER, yqHotelUserData.getStudentCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yqHotelUserData.getBuildingLevelMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yqHotelUserData.getBuildingLevelUpTimeMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yqHotelUserData.getSkillLevelMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, YqHotelUserData.parseStudentToString(yqHotelUserData.getStudentMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, YqHotelUserData.parseStudentToString(yqHotelUserData.getCacheDisappearMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yqHotelUserData.getSolderData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yqHotelUserData.getSolderTitleCountMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yqHotelUserData.getDieCountMap())));
        para.put(i++, new DbParameter(Types.BIGINT, yqHotelUserData.getLastEventTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yqHotelUserData.getEventMap())));
        para.put(i++, new DbParameter(Types.BIGINT, yqHotelUserData.getProduceTime()));
        para.put(i++, new DbParameter(Types.BIGINT, yqHotelUserData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, yqHotelUserData.getBossId()));
        para.put(i++, new DbParameter(Types.INTEGER, yqHotelUserData.getBossKillTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, yqHotelUserData.getBossBlood().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, yqHotelUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, yqHotelUserData.getUserId()));
        if (getDb().executeNoneQuery(updateYqHotelUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertYqHotelUserDataSql = "insert into `t_u_yq_hotel_user_data`(activityId,userId,integral,studentCount,buildingLevelMap,buildingLevelUpTimeMap,skillLevelMap,studentMap,cacheDisappearMap,solderData,solderTitleCountMap,dieCountMap,lastEventTime,eventMap,produceTime,updateTime,bossId,bossKillTimes,bossBlood) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(YqHotelUserData yqHotelUserData) {
        yqHotelUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, yqHotelUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, yqHotelUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, yqHotelUserData.getIntegral()));
        para.put(i++, new DbParameter(Types.INTEGER, yqHotelUserData.getStudentCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yqHotelUserData.getBuildingLevelMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yqHotelUserData.getBuildingLevelUpTimeMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yqHotelUserData.getSkillLevelMap())));
        para.put(i++, new DbParameter(Types.VARCHAR,  YqHotelUserData.parseStudentToString(yqHotelUserData.getStudentMap())));
        para.put(i++, new DbParameter(Types.VARCHAR,  YqHotelUserData.parseStudentToString(yqHotelUserData.getCacheDisappearMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yqHotelUserData.getSolderData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yqHotelUserData.getSolderTitleCountMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yqHotelUserData.getDieCountMap())));
        para.put(i++, new DbParameter(Types.BIGINT, yqHotelUserData.getLastEventTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yqHotelUserData.getEventMap())));
        para.put(i++, new DbParameter(Types.BIGINT, yqHotelUserData.getProduceTime()));
        para.put(i++, new DbParameter(Types.BIGINT, yqHotelUserData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, yqHotelUserData.getBossId()));
        para.put(i++, new DbParameter(Types.INTEGER, yqHotelUserData.getBossKillTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, yqHotelUserData.getBossBlood().toString()));
        if (getDb().executeNoneQuery(insertYqHotelUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected YqHotelUserData parseData(ResultSet rs) throws SQLException {
        YqHotelUserData yqHotelUserData = new YqHotelUserData();
        yqHotelUserData.setActivityId(rs.getInt("activityId"));
        yqHotelUserData.setUserId(rs.getLong("userId"));
        yqHotelUserData.setIntegral(rs.getLong("integral"));
        yqHotelUserData.setStudentCount(rs.getInt("studentCount"));

        String buildingStr  = rs.getString("buildingLevelMap");
        if(!StringUtils.isNullOrEmpty(buildingStr)){
            Map<Integer, Integer> result = JSON.parseObject(buildingStr, new TypeReference<ConcurrentHashMap<Integer,Integer>>() {});
            if(result != null){
                yqHotelUserData.setBuildingLevelMap(result);
            }
        }else {
            yqHotelUserData.setBuildingLevelMap(new ConcurrentHashMap<>());
        }

        String buildingLevelUpTimeStr  = rs.getString("buildingLevelUpTimeMap");
        if(!StringUtils.isNullOrEmpty(buildingLevelUpTimeStr)){
            Map<Integer, Map<Integer, Long>> result = JSON.parseObject(buildingLevelUpTimeStr, new TypeReference<ConcurrentHashMap<Integer, Map<Integer, Long>>>() {});
            if(result != null){
                yqHotelUserData.setBuildingLevelUpTimeMap(result);
            }
        }else {
            yqHotelUserData.setBuildingLevelUpTimeMap(new ConcurrentHashMap<>());
        }

        yqHotelUserData.setSkillLevelMap(JSON.parseObject(rs.getString("skillLevelMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        String studentMap = rs.getString("studentMap");
        if (StringUtils.isNullOrEmpty(studentMap)) {
            yqHotelUserData.setStudentMap(new ConcurrentHashMap<>());
        }else {
            yqHotelUserData.setStudentMap(YqHotelUserData.parseStringToStudentMap(studentMap));
        }

        String cacheDisappearMap = rs.getString("cacheDisappearMap");
        if (StringUtils.isNullOrEmpty(cacheDisappearMap)) {
            yqHotelUserData.setCacheDisappearMap(new ConcurrentHashMap<>());
        }else {
            yqHotelUserData.setCacheDisappearMap(YqHotelUserData.parseStringToStudentMap(cacheDisappearMap));
        }

        String solderStr  = rs.getString("solderData");
        if(!StringUtils.isNullOrEmpty(solderStr)){
            Map<Integer, String> result = JSON.parseObject(solderStr, new TypeReference<ConcurrentHashMap<Integer, String>>() {});
            if(result != null){
                yqHotelUserData.setSolderData(result);
            }
        }else {
            yqHotelUserData.setSolderData(new ConcurrentHashMap<>());
        }

        yqHotelUserData.setSolderTitleCountMap(JSON.parseObject(rs.getString("solderTitleCountMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        String titleCountStr  = rs.getString("solderTitleCountMap");
        if(!StringUtils.isNullOrEmpty(titleCountStr)){
            Map<Integer, Integer> result = JSON.parseObject(titleCountStr, new TypeReference<ConcurrentHashMap<Integer, Integer>>() {});
            if(result != null){
                yqHotelUserData.setSolderTitleCountMap(result);
            }
        }


        String dieCountMap = rs.getString("dieCountMap");

        if(!StringUtils.isNullOrEmpty(dieCountMap)){
            Map<Integer, Integer> result = JSON.parseObject(dieCountMap, new TypeReference<ConcurrentHashMap<Integer, Integer>>() {});
            if(result != null){
                yqHotelUserData.setDieCountMap(result);
            }
        }else {
            yqHotelUserData.setDieCountMap(new ConcurrentHashMap<>());
        }

        yqHotelUserData.setLastEventTime(rs.getLong("lastEventTime"));

        yqHotelUserData.setEventMap(JSON.parseObject(rs.getString("eventMap"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.yqhotel.data.YqHotelEventData>>() {}));
        String eventStr  = rs.getString("eventMap");
        if(!StringUtils.isNullOrEmpty(eventStr)){
            Map<Integer, YqHotelEventData> result = JSON.parseObject(eventStr, new TypeReference<ConcurrentHashMap<Integer, YqHotelEventData>>() {});
            if(result != null){
                yqHotelUserData.setEventMap(result);
            }
        }else {
            yqHotelUserData.setEventMap(new ConcurrentHashMap<>());
        }



        yqHotelUserData.setProduceTime(rs.getLong("produceTime"));
        yqHotelUserData.setUpdateTime(rs.getLong("updateTime"));
        yqHotelUserData.setBossId(rs.getInt("bossId"));
        yqHotelUserData.setBossKillTimes(rs.getInt("bossKillTimes"));
        yqHotelUserData.setBossBlood(new BigInteger(rs.getString("bossBlood")));

        return yqHotelUserData;
    }
}
